package com.wbs.shop.nuby.view.adapter;

import android.content.Context;

import androidx.recyclerview.widget.RecyclerView;

import android.view.View;
import android.widget.TextView;

import com.wbs.shop.nuby.R;
import com.wbs.shop.nuby.bean.GoodsBean;
import com.wbs.shop.nuby.bean.GoodsShowBean;
import com.wbs.shop.nuby.utils.Logger;
import com.wbs.shop.nuby.utils.StringUtils;
import com.wbs.shop.nuby.view.base.BaseRecyclerAdapter;
import com.wbs.shop.nuby.widget.AddWidget;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.subjects.PublishSubject;

/**
 * Created by zhoucl on 2018/6/13 0013.
 */
public class CarAdapter extends BaseRecyclerAdapter<CarAdapter.ViewHolder, GoodsShowBean> implements AddWidget.OnNumberChangListener {
    private double discount = 1;
    private PublishSubject mPublishSubject;

    public CarAdapter(Context mContext, PublishSubject publishSubject) {
        super(mContext);
        this.mPublishSubject = publishSubject;
    }

    public void changeDiscount(double discount) {
        if (this.discount != discount) {
            this.discount = discount;
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemLoayoutId() {
        return R.layout.item_car;
    }

    @Override
    public ViewHolder onCreateViewHolder(View rootView, int viewType) {
        return new ViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, GoodsShowBean bean, int position) {
        holder.carName.setText(bean.getName());
        try {
            holder.carPrice.setText(mContext.getString(R.string.text_money, new BigDecimal(bean.getPrice()).multiply(new BigDecimal(discount)).setScale(2, RoundingMode.HALF_UP).toEngineeringString()));
        }catch (Exception e){
            Logger.e(e);
        }
//        holder.carPrice.setText(mContext.getString(R.string.text_money, bean.getPrice()));
//        holder.carPrice.setText(mContext.getString(R.string.text_money, new BigDecimal(bean.getPrice()).multiply(BigDecimal.valueOf(bean.getQuantity()))));
        holder.carAddwidget.setTag(position);
        holder.carAddwidget.setNumber(bean.getQuantity());
        holder.carAddwidget.setOnNumberChangListener(this);
        if (StringUtils.isEmpty(bean.getBottomName())) {
            holder.carBottomName.setVisibility(View.GONE);
        } else {
            holder.carBottomName.setVisibility(View.VISIBLE);
            holder.carBottomName.setText(bean.getBottomName());
        }
    }

    public void addGoods(GoodsShowBean goodsShowBean) {
        boolean isAdd = true;
        GoodsShowBean item;
        for (int i = getItemCount() - 1; i >= 0; i--) {
            item = getItem(i);
            if (item.getId() == goodsShowBean.getId()) {
                if (
                        (goodsShowBean.getSpecId() != null &&
                                item.getSpecId() != null &&
                                goodsShowBean.getSpecId().equals(item.getSpecId()))// 购买的规格
                                ||
                                (goodsShowBean.getSpecId() == null && item.getSpecId() == null)// 购买的产品
                ) {
                    item.setQuantity(item.getQuantity() + goodsShowBean.getQuantity());
                    if (item.getQuantity() > 0) {
                        notifyItemChanged(i);
                    } else {
                        datas.remove(i);
                        notifyItemRemoved(i);
                        notifyItemRangeChanged(i, datas.size());
                    }
                    isAdd = false;
                    break;
                }
            }
        }
        if (isAdd) {
            datas.add(goodsShowBean);
            notifyItemChanged(datas.size() - 1);
        }
        if (mPublishSubject != null)
            mPublishSubject.onNext("");
    }

    @Override
    public void onNumberChang(View view, int number) {
        if (isContainPosition(view)) {
            int position = getPositionByView(view);
            if (number == 0) {
                datas.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position, datas.size());
            } else {
                GoodsShowBean goodsShowBean = getItem(position);
                goodsShowBean.setQuantity(number);
                notifyItemChanged(position);
            }
            if (mPublishSubject != null)
                mPublishSubject.onNext("");
        }
    }

    public int getTotalQuantity() {
        if (datas == null)
            return 0;
        int totalQuantity = 0;
        for (GoodsShowBean goodsShowBean : datas) {
            totalQuantity += goodsShowBean.getQuantity();
        }
        return totalQuantity;
    }

    public String getTotalAmount() {
        BigDecimal amount = new BigDecimal(0.0);
        for (GoodsShowBean goodsShowBean : datas) {
            amount = amount.add(new BigDecimal(goodsShowBean.getPrice()).multiply(BigDecimal.valueOf(goodsShowBean.getQuantity())));
        }
        return amount.toString();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.car_name)
        TextView carName;
        @BindView(R.id.car_bottom_name)
        TextView carBottomName;
        @BindView(R.id.car_price)
        TextView carPrice;
        @BindView(R.id.addwidget)
        AddWidget carAddwidget;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
